package com.etime.shuhuiyuan.services.teacher.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.TeacherDao;
import com.etime.shuhuiyuan.dao.TeacherDtoDao;
import com.etime.shuhuiyuan.dao.UserDao;
import com.etime.shuhuiyuan.dto.TeacherDto;
import com.etime.shuhuiyuan.pojo.Teacher;
import com.etime.shuhuiyuan.pojo.User;
import com.etime.shuhuiyuan.services.teacher.interfaces.TeacherService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TeacherServiceImpl2 implements TeacherService2{

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private UserDao userDao;

    @Autowired
    private TeacherDtoDao teacherDtoDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTeacher(Teacher teacher,User user) {
        int rtn=0;
        if (user!=null && user.getUsername()!=null && !user.getUsername().equalsIgnoreCase("")){
            user.setIsteacher(1);
            userDao.insert(user);
            int userid=user.getUserid();
            teacher.setUserid(userid);
            //int i=1/0;
            rtn = teacherDao.insert(teacher);
        }
        return rtn;
    }


    @Override
    public int updateTeacher(Teacher teacher) {
        return teacherDao.updateById(teacher);
    }

    @Override
    public List<Teacher> selectTeacherList(Teacher teacher) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        if (teacher != null && teacher.getTectitle() != null && !teacher.getTectitle().equalsIgnoreCase("")){
            wrapper.eq("tectitle",teacher.getTectitle());
        }
        return teacherDao.selectList(wrapper);
    }


    @Override
    public Page<Teacher> selectTeacherByPage(Teacher teacher, long pageNum, long pageSize) {
        Page<Teacher> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        if (teacher!=null && teacher.getTectitle()!=null && !teacher.getTectitle().equalsIgnoreCase("")){
            wrapper.eq("tectitle",teacher.getTectitle());
        }
        return teacherDao.selectPage(page,wrapper);
    }
    @Override
    public Page<TeacherDto> selectTeacherDtoByPage(TeacherDto teacherDto, long pageNum, long pageSize) {
        Page<TeacherDto> page = new Page<>(pageNum,pageSize);
        String usertruename="";
        Integer state=null;
        if (teacherDto!=null ){
            if( teacherDto.getUsertruename()!=null && !teacherDto.getUsertruename().equalsIgnoreCase("")){
                usertruename=teacherDto.getUsertruename();
                usertruename="%"+usertruename+"%";
            }
            if(teacherDto.getTecauditstate()!=null && teacherDto.getTecauditstate()!=-1){
                state=teacherDto.getTecauditstate();
            }
        }
        return teacherDtoDao.selectTeacherDtoByPage(page,teacherDto,usertruename,state);
    }

    @Override
    public Teacher selectTeacherById(Integer teacherId) { return teacherDao.selectById(teacherId); }

    @Override
    public TeacherDto selectTeacherDtoById(Integer teacherDtoId) {
        return teacherDtoDao.selectTeacherDtoById(teacherDtoId);
    }


    @Override
    public List<TeacherDto> selectTeacherDtoList(TeacherDto teacherDto) {
        String usertruename = "";
        if (teacherDto!=null && teacherDto.getUsertruename() !=null && !teacherDto.getUsertruename().equalsIgnoreCase("")){
            usertruename=teacherDto.getUsertruename();
            usertruename="%"+usertruename+"%";
        }
        return teacherDtoDao.selectTeacherDtoList(teacherDto,usertruename);
    }



}
